import java.util.Arrays;

public class MyArraryList {
    private int[]arr;
    int usesize;
    public MyArraryList(){
        arr=new int[10];
    }
    public void add(int val){
        expansion();
        arr[usesize++]=val;
    }
    public void add(int pos,int val)throws indexException{
        expansion();
        if(pos<0||pos>usesize){
            throw new indexException("下标异常");
        }
        for (int i = usesize-1; i>=pos ; i--) {
            arr[i+1]=arr[i];
        }
        arr[pos]=val;
        usesize++;
    }
    //删除哪个元素的下标
    public boolean remove(int pos)throws indexException{
        if(empty()){
            return false;
        }
        if(pos<0||pos>=usesize){
            throw new indexException("下标异常");
        }
        for (int j =pos;j<usesize-1; j++) {
            arr[j]=arr[j+1];
        }
        usesize--;
        return true;
    }
    public boolean isfull(){
        return arr.length==usesize;
    }
    public boolean empty(){
        return usesize==0;
    }
    //扩容
    public  void expansion(){
        if(isfull()){
            arr= Arrays.copyOf(arr,2*arr.length);
        }
    }
    //找到最大值，返回下标
    public int Max(){
        if(empty()){
            return -1;
        }
        int index=0;
        int max=arr[0];
        for (int i = 1; i < usesize; i++) {
            if(max<arr[i]){
                max=arr[i];
                index=i;
            }
        }
        return index;
    }
    public int Min(){
        if(empty()){
            return -1;
        }
        int index=0;
        int min=arr[0];
        for (int i = 1; i < usesize; i++) {
            if(min>arr[i]){
                min=arr[i];
                index=i;
            }
        }
        return index;
    }

    //翻转数组
    public void reserve(){
        if(empty()){
            return ;
        }
        for (int i = 0; i < usesize/2; i++) {
            int tmp=arr[i];
            arr[i]=arr[usesize-1-i];
            arr[usesize-1-i]=tmp;
        }
    }

    public void func(){
        if(empty()){
            return ;
        }
        int indexmax=Max();
        int indexmin=Min();
        int t=arr[indexmax];
        if(indexmin<indexmax){
            remove(indexmax);
            add(indexmin+1,t);
        }else{
            remove(indexmax);
            add(indexmin,t);
        }



    }

    public void dissplay(){
        for (int i = 0; i < usesize; i++) {
            System.out.print(arr[i]+" ");
        }
    }
}
